#include "wind_speed.h"

uint16_t WS_GetData(uint8_t address)
{
    uint8_t time = 10;
    uint16_t data = 0;
    dataTypeChange_t crc_check;
    uint8_t dataPack[8] = {address, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
    
    crc_check.u16[0] = CalculateCRC(dataPack, 6);  //计算CRC检验码
    dataPack[6] = crc_check.u8[0];
    dataPack[7] = crc_check.u8[1];
    
    Serial_SendArray(dataPack, 8);  //发送查询帧
    while(time && !Serial_GetRxFlag())
    {
        delay_ms(10);
        time--;
        if(time == 0)
        {
            Serial.length = 0;
            return  0;
        }
    }
    crc_check.u16[0] = CalculateCRC(Serial.payload, Serial.length-2);    //计算应答数据的CRC校验码
    if((crc_check.u8[0]==Serial.payload[Serial.length-2]) && (crc_check.u8[1]==Serial.payload[Serial.length-1]))
    {
        data = (uint16_t)Serial.payload[3] << 8;    //获取风速高八位
        data |= (uint16_t)Serial.payload[4];        //获取风速第八位
        Serial.length = 0;
        return data;
    }
    return 0;
}

